OrderWin = {
	columns:[],
	rowNum:0
};

OrderWin.styles = {
	titleColor:'white'
};

OrderWin.getSignature = function() {
	return OrderWin.signatureView.evalJS('getSignature()');
};

OrderWin.createVLine = function(left, color) {
	var vLine = Ti.UI.createView({
		width:1,
		left:left,
		borderWidth:1,
		borderColor:color
	});
	return vLine;
};

OrderWin.getColumn = function(id) {
	return OrderWin.columns[id];
};

OrderWin.getRow = function(data) {
	OrderWin.rowNum++;
	
	var row = Ti.UI.createTableViewRow({
		height:30
	});
	if (OrderWin.rowNum % 2 == 0) {
		row.backgroundColor = '999';
	}
	
	function createField(id, data) {
		var column = OrderWin.getColumn(id);
		var lbl = Ti.UI.createLabel({
			text:data,
			textAlign:'right',
			width:column.lbl.width - 10,
			left:column.leftLine ? column.leftLine.left : 0
		});
		// dernière colonne
		if (!column.lbl.width) {
			lbl.width = 'auto';
			lbl.right = 10;
			lbl.left = null;
		}
		row.add(lbl);
	}
	
	createField('ref', '10132000');
	createField('des', 'DIABLO FIRE ULTRAFIT');
	createField('qty', 19);
	createField('price', formatPrice(198));
	createField('disc', formatFloat(10));
	createField('sum', formatPrice(3385.8));
	
	// ajout des lignes verticales
	for (var column in OrderWin.columns) {
		if (column) {
			var vLine = OrderWin.getColumn(column).leftLine;
			if (vLine) {
				row.add(OrderWin.createVLine(vLine.left, 'black'));
			}
		}
	}
	
	return row;
};

OrderWin.initTableView = function() {
	var tvData = [];
	
	var section = Ti.UI.createTableViewSection();
	var header = Ti.UI.createView({
		height:30,
		backgroundColor:'black'
	});
	section.headerView = header;
	
	var firstTitle = true;
	var prevTitle, vLine;
	function createTitle(id, title, width) {
		OrderWin.columns[id] = [];
		
		if (!firstTitle) {
			vLine = OrderWin.createVLine(right(prevTitle), 'white');
			header.add(vLine);
			
			OrderWin.getColumn(id).leftLine = vLine;
		}
		firstTitle = false;
		
		var lbl = Ti.UI.createLabel({
			text:title,
			color:OrderWin.styles.titleColor,
			font:{fontSize:18, fontWeight:'bold'},
			textAlign:'center',
			width:width,
			left:vLine ? vLine.left + 1 : 0
		});
		header.add(lbl);
		OrderWin.getColumn(id).lbl = lbl;
		prevTitle = lbl;
		
		return lbl;
	}
	
	createTitle('ref', 'Référence', 200);
	createTitle('des', 'Désignation', 400);
	createTitle('qty', 'Quantité', 100);
	createTitle('price', 'Prix', 100);
	createTitle('disc', 'Remise', 100);
	createTitle('sum', 'Montant');
	
	section.add(OrderWin.getRow());
	section.add(OrderWin.getRow());
	section.add(OrderWin.getRow());
	section.add(OrderWin.getRow());
	section.add(OrderWin.getRow());
	tvData.push(section);
	
	var tableView = Ti.UI.createTableView({
		data:tvData,
		bottom:OrderWin.toolbar.height
	});
	return tableView;
};

OrderWin.initSignatureView = function() {
	var getSignatureView;
	var signatureView = Ti.UI.createWebView({
		bottom:OrderWin.toolbar.height,
		right:0,
		height:100,
		width:100
	});
	
	signatureView.url = 'signature.html';
	
	var cancel = Ti.UI.createButton({
		title:'Annuler'
	});
	cancel.addEventListener('click', function(e) {
		popover.hide({animated:true});
	});
	
	var validate = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Valider'
	});
	validate.addEventListener('click', function(e) {
		var signature = getSignatureView.evalJS('getSignature()');
		Ti.API.info(signature);
		signatureView.evalJS("setSignature('" + signature + "')");
		popover.hide({animated:true});
	});
	
	var popover = Ti.UI.iPad.createPopover({ 
		//arrowDirection:Ti.UI.iPad.POPOVER_ARROW_DIRECTION_TOP,
		leftNavButton:cancel,
		rightNavButton:validate,
		width:600,
		height:600
	});
	
	signatureView.addEventListener('click', function(e) {
		getSignatureView = Ti.UI.createWebView();
		getSignatureView.url = 'signature.html';
		popover.add(getSignatureView);
		
		popover.show({
			view:signatureView,
			animated:true
		});
	});
	
	return signatureView;
};

OrderWin.initToolbar = function() {
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	
	var btCancel = Ti.UI.createButton({
		title:'Annuler',
		bottom:0,
		left:0,
		width:80,
		height:30
	});
	btCancel.addEventListener('click', function(e) {
	});
	
	var btSave = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Valider',
		bottom:0,
		right:0,
		width:80,
		height:30
	});
	btSave.addEventListener('click', function(e) {
		Ti.API.info(OrderWin.getSignature());
	});
	
	var toolbar = Titanium.UI.createToolbar({
		items:[flexSpace, btCancel, flexSpace, btSave, flexSpace],
		bottom:0,
		borderTop:true,
		borderBottom:false,
		translucent:true,
		barColor:'#999'
	});
	
	return toolbar;
};

OrderWin.open = function() {
	var win = createWindow({
		title:'Signature'
	});
	
	OrderWin.toolbar = OrderWin.initToolbar();
	win.add(OrderWin.initTableView());
	win.add(OrderWin.initSignatureView());
	win.add(OrderWin.toolbar);
	win.open({transition:Ti.UI.iPhone.AnimationStyle.CURL_UP});
};
